﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YJKRevitBase.WinForm;
using YJKRevitDimensionText.OneKeyPlanDim.DimData;
using YJKRevitDimensionText.OneKeyPlanDim.LevelDim;
using YJKRevitDimensionText.OneKeyPlanDim.OutWallThreeDim;
using YJKRevitDimensionText.OneKeyPlanDim.Utility;

namespace YJKRevitDimensionText.OneKeyPlanDim
{
	public class PlanDimensionCreateManage
	{
		public PlanDimensionCreateManage(ExternalCommandData commandData)
		{
			this.m_CommandData = commandData;
			this.m_Uiapp = commandData.Application;
			this.m_Uidoc = this.m_Uiapp.ActiveUIDocument;
			this.m_doc = this.m_Uidoc.Document;
		}

		public bool Run()
		{
			this.m_OneKeyPlanDim = new OneKeyPlanDimForm();
			if (this.m_OneKeyPlanDim.ShowDialog() != DialogResult.OK)
			{
				return false;
			}
			if (!this.TryGetPlanDimensionData())
			{
				return false;
			}
			using (TransactionGroup transactionGroup = new TransactionGroup(this.m_doc))
			{
				try
				{
					transactionGroup.Start("TransGroupCreateModelGroup");
					if (!this.TryMakeDim())
					{
						YJKMessageBox.Show("标注失败!");
						transactionGroup.RollBack();
						return false;
					}
					transactionGroup.Assimilate();
					YJKMessageBox.Show("标注完成!");
				}
				catch
				{
                    if ((int)transactionGroup.GetStatus() == 1)
					{
						transactionGroup.RollBack();
					}
				}
			}
			return true;
		}

		private bool TryGetPlanDimensionData()
		{
			this.m_PlanDimensionDataManage = new PlanDimensionDataManage(this.m_CommandData, this.m_doc);
			return this.m_PlanDimensionDataManage.TryPreparePlanDimensionData();
		}

		private bool TryMakeDim()
		{
			if (this.m_HWndRevit == null)
			{
				IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
				this.m_HWndRevit = new WindowHandle(mainWindowHandle);
			}
			int num = 20;
			FamilySymbol slopeDimFamilySymbol = new SlopeDimData(this.m_doc).GetSlopeDimFamilySymbol();
			if (slopeDimFamilySymbol == null)
			{
				num -= 7;
			}
			FamilySymbol levelDimFamilySymbol = new LevelDimData(this.m_doc).GetLevelDimFamilySymbol();
			if (levelDimFamilySymbol == null)
			{
				num -= 4;
			}
			SizeDimData.GetLevelDimFamilySymbol(this.m_doc);
			ProgressBarForm progressBarForm = new ProgressBarForm(num, ProgressBarStyle.Blocks, true);
			progressBarForm.Text = "正在进行标注创建...";
			progressBarForm.ShowInTaskbar = false;
			progressBarForm.Show(this.m_HWndRevit);
			try
			{
				if (slopeDimFamilySymbol != null)
				{
					this.TryMakeRampByDoorSlopeDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeSlopeFloorSlopeDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeRevitRampSlopeDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeBarrierFreeRampSlopeDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeCarRampSlopeDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
				}
				if (OneKeyPlanDimSelctionDim.m_bOutArchSize || OneKeyPlanDimSelctionDim.m_bInArchSize)
				{
					DrawPlanDim drawPlanDim = new DrawPlanDim(this.m_CommandData.Application);
					if (OneKeyPlanDimSelctionDim.m_bOutArchSize)
					{
						drawPlanDim.DrawThreeDim();
						drawPlanDim.DrawOutWallDWDimFun();
					}
					if (OneKeyPlanDimSelctionDim.m_bInArchSize)
					{
						drawPlanDim.DrawInWallDWDimFun();
						drawPlanDim.DrawInWallThickDimFun();
					}
				}
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeStepSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeRampByDoorSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeSlopeFloorSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeRevitRampSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeBarrierFreeRampSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeCarRampSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeCanopySizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				this.TryMakeFloorDrainSizeDim();
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					progressBarForm.Dispose();
					return false;
				}
				if (levelDimFamilySymbol != null)
				{
					this.TryMakeParapetLevelDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeRoofLevelDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeFloorLevelDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeOutDoorFloorLevelDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
				}
				if (slopeDimFamilySymbol != null)
				{
					this.TryMakeRoofRampDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
					this.TryMakeFloorDrainLevelDim();
					progressBarForm.RefreshProgressBar();
					if (progressBarForm.m_stopTag)
					{
						progressBarForm.Dispose();
						return false;
					}
				}
			}
			catch (Exception)
			{
				progressBarForm.Dispose();
				return false;
			}
			return true;
		}

		private List<ElementId> CreateThreeSizeDim()
		{
			TransactionGroup transactionGroup = new TransactionGroup(this.m_doc, "grp");
			transactionGroup.Start();
			new OutWallThreeDimManager(this.m_doc, this.m_Uiapp, this.m_doc.ActiveView, null);
			List<ElementId> result = null;
			transactionGroup.Assimilate();
			return result;
		}

		private bool TryMakeRampByDoorSlopeDim()
		{
			foreach (DbRampByDoor dbRampByDoor in KeyComponent.GetInstance().m_AllDbRampByDoor)
			{
				dbRampByDoor.CreateSlopeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeSlopeFloorSlopeDim()
		{
			foreach (DbSlopeFloor dbSlopeFloor in KeyComponent.GetInstance().m_AllDbSlopeFloor)
			{
				dbSlopeFloor.CreateSlopeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeRevitRampSlopeDim()
		{
			foreach (DbRevitRamp dbRevitRamp in KeyComponent.GetInstance().m_AllDbRevitRamp)
			{
				dbRevitRamp.CreateSlopeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeBarrierFreeRampSlopeDim()
		{
			foreach (DbBarrierFreeRamp dbBarrierFreeRamp in KeyComponent.GetInstance().m_AllDbBarrierFreeRamp)
			{
				dbBarrierFreeRamp.CreateSlopeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeCarRampSlopeDim()
		{
			foreach (DbCarRamp dbCarRamp in KeyComponent.GetInstance().m_AllDbCarRamp)
			{
				dbCarRamp.CreateSlopeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeRoofSlopeDim()
		{
			return true;
		}

		private bool TryMakeFloorDrainSlopeDim()
		{
			return true;
		}

		private bool TryMakeStepSizeDim()
		{
			foreach (DbSteps dbSteps in KeyComponent.GetInstance().m_AllDbSteps)
			{
				dbSteps.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeRampByDoorSizeDim()
		{
			foreach (DbRampByDoor dbRampByDoor in KeyComponent.GetInstance().m_AllDbRampByDoor)
			{
				dbRampByDoor.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeSlopeFloorSizeDim()
		{
			foreach (DbSlopeFloor dbSlopeFloor in KeyComponent.GetInstance().m_AllDbSlopeFloor)
			{
				dbSlopeFloor.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeRevitRampSizeDim()
		{
			foreach (DbRevitRamp dbRevitRamp in KeyComponent.GetInstance().m_AllDbRevitRamp)
			{
				dbRevitRamp.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeBarrierFreeRampSizeDim()
		{
			foreach (DbBarrierFreeRamp dbBarrierFreeRamp in KeyComponent.GetInstance().m_AllDbBarrierFreeRamp)
			{
				dbBarrierFreeRamp.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeCarRampSizeDim()
		{
			foreach (DbCarRamp dbCarRamp in KeyComponent.GetInstance().m_AllDbCarRamp)
			{
				dbCarRamp.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeCanopySizeDim()
		{
			foreach (DbCanopy dbCanopy in KeyComponent.GetInstance().m_lstDbCanopy)
			{
				dbCanopy.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeFloorDrainSizeDim()
		{
			foreach (DbGutter dbGutter in KeyComponent.GetInstance().m_AllDbGutter)
			{
				dbGutter.CreateSizeDim(this.m_doc);
			}
			return true;
		}

		private bool TryMakeFloorDrainLevelDim()
		{
			if (KeyComponent.GetInstance().m_AllDbFloorDrain != null)
			{
				foreach (DbFloorDrain dbFloorDrain in KeyComponent.GetInstance().m_AllDbFloorDrain)
				{
					dbFloorDrain.CreateSlopeDim(this.m_doc);
				}
			}
			return true;
		}

		private bool TryMakeRoofRampDim()
		{
			if (KeyComponent.GetInstance().m_lstRoof != null)
			{
				new DbRoofRamp(this.m_doc)
				{
					LstRoofBase = KeyComponent.GetInstance().m_RoofLevelDim.LstRoofBase,
					LstFloor = KeyComponent.GetInstance().m_RoofLevelDim.LstFloor
				}.CreateSlopeDim();
			}
			return true;
		}

		private bool TryMakeParapetLevelDim()
		{
			if (KeyComponent.GetInstance().m_ParapetLevelDim != null)
			{
				KeyComponent.GetInstance().m_ParapetLevelDim.CreateLevelDim();
			}
			return true;
		}

		private bool TryMakeRoofLevelDim()
		{
			if (KeyComponent.GetInstance().m_RoofLevelDim != null)
			{
				KeyComponent.GetInstance().m_RoofLevelDim.CreateLevelDim();
			}
			return true;
		}

		private bool TryMakeFloorLevelDim()
		{
			if (KeyComponent.GetInstance().m_floorLevelDim != null)
			{
				KeyComponent.GetInstance().m_floorLevelDim.CreateLevelDim();
			}
			return true;
		}

		private bool TryMakeOutDoorFloorLevelDim()
		{
			if (KeyComponent.GetInstance().m_OutDoorFloorLevelDim != null)
			{
				KeyComponent.GetInstance().m_OutDoorFloorLevelDim.CreateLevelDim();
			}
			return true;
		}

		private PlanDimensionDataManage m_PlanDimensionDataManage;

		private ExternalCommandData m_CommandData;

		private UIApplication m_Uiapp;

		private UIDocument m_Uidoc;

		private Document m_doc;

		private OneKeyPlanDimForm m_OneKeyPlanDim;

		private List<ElementId> m_lstThreeSizeDim = new List<ElementId>();

		private WindowHandle m_HWndRevit;
	}
}
